<template>
    <div>
        <div v-if="currentPanel == 'list'">
            <el-card class="box-card">
                <!--            <el-button type="primary" @click="currentHandle('add')" size="small">+新建欢迎语</el-button>-->
                <el-button type="primary" @click="createGreetHandle()" size="small">+新建欢迎语</el-button>

                <div class="interspaceH"></div>

                <el-table :data="dataSource" style="width: 100%"
                          :header-cell-style="{background:'#F3F4F7',color:'#555'}">
                    <el-table-column prop="welcomeMessage" label="消息内容" :formatter="formatter">
                        <template slot-scope="scope">
<!--                            <img v-if="scope.row.messageType == '2' || scope.row.messageType == '4'" style="height: 88px" :src="$uri + scope.row.welcomeMessage" class="avatar">-->
                            <span>{{ scope.row.welcomeMessage == "" || scope.row.welcomeMessage == null ? "--" : scope.row.welcomeMessage}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="使用成员">
                        <template slot-scope="scope">
                            <span>全部成员</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="createPerple" label="创建人" :formatter="formatter"></el-table-column>
                    <el-table-column prop="createTime" label="创建时间" :formatter="formatter"></el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button @click="detailHandle(scope.row)" type="text" size="small">详情</el-button>
                            <el-divider direction="vertical"></el-divider>
                            <el-button @click="updateHandle(scope.row)" type="text" size="small">编辑</el-button>
                            <el-divider direction="vertical"></el-divider>
                            <el-button @click="deleteHandle(scope.row)" type="text" size="small">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <div class="interspaceH"></div>

                <el-pagination @size-change="queryDataHandle(pagePagination.currentPage,$event)"
                               @current-change="queryDataHandle($event,pagePagination.pageSize)"
                               :current-page="pagePagination.currentPage"
                               :page-sizes="pagePagination.pageSizes" :page-size="pagePagination.pageSize"
                               layout="total, sizes, prev, pager, next"
                               :total="pagePagination.total">
                </el-pagination>
            </el-card>
        </div>
        <div v-if="currentPanel == 'add'">
            <SaveGreeting @currentHandle="currentHandle"/>
        </div>
        <div v-if="currentPanel == 'edit'">
            <AddGreeting @currentHandle="currentHandle"/>
        </div>

        <el-dialog title="详情" :visible.sync="detailVisible" width="30%">
            <el-form ref="form" :model="detailForm" label-position="left" label-width="80px">
                <el-form-item label="消息内容">
                    <el-input v-model="detailForm.welcomeMessage" disabled></el-input>
                </el-form-item>
                <el-form-item label="使用成员">
                    <el-input disabled value="全部成员"></el-input>
                </el-form-item>
                <el-form-item label="创建人">
                    <el-input v-model="detailForm.createPerple" disabled></el-input>
                </el-form-item>
                <el-form-item label="创建时间">
                    <el-input v-model="detailForm.createTime" disabled></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="detailVisible = false">返回</el-button>
            </span>
        </el-dialog>

        <el-dialog :title="formType == 'add' ? '创建欢迎语' : '修改欢迎语'" :visible.sync="createVisible" width="30%">
<!--            <el-form ref="form" :model="greetForm" label-position="left" label-width="80px">
                <el-card class="greetContent">
                    <div slot="header" class="clearfix">
                        <div style="display:flex;align-items: center;">
                            <div style="font-size: 14px;">插入：</div>
                            <el-button @click="greetForm.welcomeMessage += '#客户名称#'" type="text">客户昵称</el-button>
                        </div>
                    </div>
                    <div>
                        <el-input
                                style="width: 100%;height: 100%;"
                                type="textarea"
                                placeholder="请输入内容"
                                v-model="greetForm.welcomeMessage"
                                maxlength="300"
                                show-word-limit>
                        </el-input>
                    </div>
                </el-card>
            </el-form>-->
            <el-form>
                <el-form-item label="欢迎语内容" prop="sendContent">
                    <el-button type="text" @click="greetForm.welcomeMessage += '#客户昵称#'">客户昵称</el-button>
                    <el-input type="textarea" placeholder="请输入内容" v-model="greetForm.welcomeMessage" maxlength="1200"
                              show-word-limit>
                    </el-input>
                </el-form-item>
            </el-form>

            <MaterialContentText ref="materialContentText" :disabledAdd="true" :contentType="[2]"/>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitHandle">提交</el-button>
                <el-button @click="createVisible = false">返回</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import AddGreeting from "@/views/customer/client_greeting/add";
import SaveGreeting from "@/views/customer/client_greeting/save"
import MaterialContentText from "@/components/material_content_text/material_content_text"

export default {
    components: {AddGreeting, SaveGreeting,MaterialContentText},
    data() {
        return {
            tablePage: 0,
            currentPanel: 'list',
            formType: '',
            detailVisible: false,
            createVisible: false,
            dictionary: {},
            fieldType: {
                updateTime: "datetime",
                createTime: "datetime"
            },
            greetForm: {
                welcomeMessage: ""
            },
            detailForm: {
                messageContent: '',
                useMember: '',
                updateTime: '',
                createTime: '',
            },
            dataSource: [],
            pagePagination: {
                currentPage: 1,
                pageSizes: [10, 15, 20, 25],
                pageSize: 10,
                total: 0
            }
        }
    },
    mounted() {
        this.queryDataHandle()
    },
    methods: {
        currentHandle(e) {
            this.currentPanel = e
        },
        createGreetHandle() {
            this.formType = 'add'
            this.greetForm = {
                welcomeMessage: ""
            }
            this.createVisible = true
        },
        queryDataHandle(page = 1, row = 10) {
            let that = this;
            let payload = {
                welcomeType: "2",
                pageNum: page,
                pageSize: row
            }
            that.$request("/api/welcome/selectAll/1_0", payload, "POST").then((data) => {
                that.dataSource = data.rows
                that.pagePagination['total'] = data.total
                that.pagePagination['pageSize'] = data.row
            })
        },
        formatter(row, column) {
            let field = column.property;
            let value = row[field];
            return this.$utils.formatterForLable(field, value, this.fieldType, this['dictionary'][field]);
        },
        deleteHandle(row) {
            this.$confirm('是否确认删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$request("/api/welcome/deleteWelcome/1_0", {welcomeId: row.welcomeId}, "POST").then((data) => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.queryDataHandle()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        detailHandle(row) {
            this.detailForm = this.$utils.formatterForForm(row, this.fieldType, this.dictionary);
            this.detailVisible = true
        },
        updateHandle(row) {
            this.formType = 'edit'
            this.greetForm['welcomeId'] = row.welcomeId
            this.greetForm.welcomeMessage = row.welcomeMessage
            this.createVisible = true
        },
        submitHandle() {
            let greetForm = this.greetForm

            if (greetForm.welcomeMessage == null || greetForm.welcomeMessage == "") {
                this.$message.error("请输入欢迎语内容")
                return
            }

            let content = this.$refs.materialContentText.getData()

            let payload = {
                welcomeType: "2",
                createPerple: this.$store.getters.getUserInfo.userName,
                welcomeMessage: this.greetForm.welcomeMessage,
            }

            if (content != null && content.length > 0) {
                if (content[0]['contentType'] == '2') {
                    if (content[0]['material'] != null) {
                        if (content[0]['material']['materialType'] == '2') {
                            payload['messageType'] = 4
                            payload['welcomeAppendices'] = content[0]['material']['materialLinksCover']
                        } else {
                            payload['messageType'] = 2
                            payload['welcomeAppendices'] = content[0]['material']['materialAppendices']
                        }
                    }
                } else if (content[0]['contentType'] == '3') {
                    payload['messageType'] = 1
                    payload['welcomeAppendices'] = content[0]['linkContent']['linkAddress']
                }
            }

            let url = ""
            if (this.formType == 'edit') {
                payload['welcomeId'] = greetForm.welcomeId
                url = "/api/welcome/updateWelcome/1_0"
            } else {
                url = "/api/welcome/addWelcome/1_0"
            }
            this.$request(url, payload, "POST").then((data) => {
                this.$message.success("好友欢迎语" + this.formType == 'add' ? '创建' : '修改' + "成功！")
                this.createVisible = false
                this.queryDataHandle()
            })
        }
    }
}
</script>

<style>
.greetContent .el-card__header {
    padding: 0 10px;
}

.greetContent .el-card__body {
    padding: 0;
}

.greetContent .el-textarea .el-textarea__inner {
    min-height: 120px !important;
}
</style>
